package com.collectplus.express.parcel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Random;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.OrderRouterBean;
import com.collectplus.express.tools.DensityUtils;

import droid.frame.activity.base.BaseAdapterExt;

@SuppressLint("SimpleDateFormat")
public class ParcelRouteAdapter extends BaseAdapterExt<OrderRouterBean> {

	private SimpleDateFormat forma3;

	public ParcelRouteAdapter(ArrayList<OrderRouterBean> items, Activity context) {
		super(items, context);
		forma3 = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
	}

	private Context getContext() {
		return context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || convertView.getTag() == null) {
			holder = new ViewHolder();
			convertView = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.parcel_detail_routes_item, parent, false);
			holder.transportImg = (ImageView) convertView.findViewById(R.id.parcel_detail_item_img);
			holder.updateTime = ((TextView) convertView.findViewById(R.id.parcel_detail_item_time));
			holder.desc = ((TextView) convertView.findViewById(R.id.parcel_detail_item_route));
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		OrderRouterBean item = getItem(position);
		holder.updateTime.setText(forma3.format(item.getUpdateTime()));
		holder.desc.setText(item.getDescription());
		if (position == 0) {
			((LinearLayout) convertView.findViewById(R.id.route_item_left_layout)).setPadding(DensityUtils.dip2px(getContext(), 28), DensityUtils.dip2px(getContext(), 19), 0, 0);
			((LinearLayout) convertView.findViewById(R.id.route_item_right_layout)).setPadding(DensityUtils.dip2px(getContext(), 21), DensityUtils.dip2px(getContext(), 19),
					DensityUtils.dip2px(getContext(), 21), DensityUtils.dip2px(getContext(), 33));
		}
		if (getItemsSize() == 1) {
			holder.transportImg.setImageResource(R.drawable.parcel_detail_dot2);
		} else {
			if (position == 0) {
				holder.transportImg.setImageResource(R.drawable.parcel_detail_dot1);
			} else if (position == 1) {
				holder.transportImg.setImageResource(R.drawable.parcel_detail_dot2);
			} else {
				// 中间图片随机展示
				int randomInt = new Random().nextInt(12) % 10 + 3;
				int resId = getContext().getResources().getIdentifier("parcel_detail_dot" + randomInt, "drawable", getContext().getPackageName());
				holder.transportImg.setImageResource(resId);
			}
		}
		return super.getView(position, convertView, parent);
	}

	private class ViewHolder {
		TextView updateTime;
		TextView desc;
		ImageView transportImg;
	}
}
